package com.security.web.rest;

import com.fasterxml.jackson.annotation.JsonView;
import com.security.dto.QueryCondition;
import com.security.dto.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author roger
 * @email 190642964@qq.com
 * @create 2017-11-01 10:56
 **/
@RestController
@RequestMapping("/user")
public class UserRest {

    /**
     * 获取当前登陆用户（所有信息）
     * @return Object
     */
    @RequestMapping("/me")
    public Object getCurrentUser(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前登陆用户（所有信息）
     * @return Object
     */
    @RequestMapping("/me1")
    public Object getCurrentUser1(Authentication authentication){
        return authentication;
    }

    /**
     * 获取当前登陆用户（所有信息）
     * @return Object
     */
    @RequestMapping("/me2")
    public Object getCurrentUser2(@AuthenticationPrincipal UserDetails userDetails){
        return userDetails;
    }

    /**
     * 用户查询接口
     *
     * @param condition QueryCondition
     * @param pageable  Pageable
     * @return List<User>
     */
    @GetMapping
    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "查询列表")
    public List<User> query(QueryCondition condition, @PageableDefault(page = 1, size = 10, sort = "name,asc") Pageable pageable) {
        System.out.println(condition);
        System.out.println(pageable);
        List<User> users = new ArrayList<>();
        users.add(new User(1L, "AAA", "ad"));
        users.add(new User(2L, "BBB", "bd"));
        users.add(new User(3L, "CCC", "cd"));
        return users;
    }

    /**
     * 获取详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId:\\d+}")
    @JsonView(User.UserDetailView.class)
    @ApiOperation(value = "查询详情")
    public User queryInfo(@ApiParam("主键ID") @PathVariable long userId) {
//        throw new CustomException("user not exit");
//        throw new RuntimeException("123456");
        System.out.println(" queryInfo userId : " + userId);
        return new User(userId, "tom", "psd");
    }

    /**
     * 创建
     *
     * @param user
     * @return
     */
    @PostMapping
    public User create(@Valid @RequestBody User user) {
        System.out.println(user);
        user.setId(1L);
        return user;
    }

    /**
     * 创建
     *
     * @param user
     * @param errors
     * @return
     */
   /* @PostMapping
    public User create(@Valid @RequestBody User user, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().stream().forEach(error -> System.out.println(error.getDefaultMessage()));
        }
        System.out.println(user);
        user.setId(1L);
        return user;
    }*/
    @PutMapping("/{userId:\\d+}")
    public User modify(@Valid @RequestBody User user, BindingResult errors) {
        if (errors.hasErrors()) {
            errors.getAllErrors().stream().forEach(error -> {
//                FieldError fieldError = (FieldError) error;
//                String message = fieldError.getField() + " " + error.getDefaultMessage();
                System.out.println(error.getDefaultMessage());
            });
        }
        System.out.println(user);
        user.setId(1L);
        return user;
    }

    @DeleteMapping("/{userId:\\d+}")
    @JsonView(User.UserDetailView.class)
    public void delete(@PathVariable long userId) {
        System.out.println("userId : " + userId);
    }


}
